# -*- coding:utf-8 -*-

import os
import unittest
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.ui import Select
#from .WorkFlow.User import User
#from .WorkFlow.WorkFlow import WorkFlow
from extScript.Excel import RdExcel
from extScript.ParseBDCType import BDCType
from extScript.ReadTextFile import ReadTestFile
from extScript.UploadPic import UploadPic


class QTZZ(unittest.TestCase):
    def setUp(self):
        #self.driver = webdriver.Firefox()

        iedriver = os.getcwd()+"\\IEDriverServer.exe"
        os.environ["webdriver.ie.driver"] = iedriver
        self.driver = webdriver.Ie(executable_path=iedriver)

        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.base_url = "http://192.168.200.248:8080"
        self.verificationErrors = []
        self.accept_next_alert = True

        self.casedate = RdExcel()
        self.caselist = self.casedate.getUserCaseByExcel("其它组织")

        self.driver.get(self.base_url + "/RealestateTrade")
        User().Login(self.driver)
        self.dbt = BDCType()


    def test_qtzztester(self):
        driver = self.driver
        list = self.caselist

        for dist in list:
            dist = list[5]
            sleep(1)
            try:
                driver.find_element_by_xpath("/html/body/header/div[2]/ul[1]/li[1]/a").click()
            except Exception as e:
                sleep(2)
                driver.find_element_by_xpath("/html/body/header/div[2]/ul[1]/li[1]/a").click()
            self.qtzz(driver,dist)

    def qtzz(self,driver,dist):
        # 选择委托类型
        try:
            driver.find_element_by_link_text(dist["委托类型"]).click()
        except:
            self.movedownPage(driver, elementXPATH="/html/body/content/div/div[3]/div[2]/div[2]/ul/li[3]/a/h2")
            driver.find_element_by_link_text(dist["委托类型"]).click()

        Select(driver.find_element_by_id("entrustedPersons")).select_by_visible_text(dist["权利人类型"])

        # 是否委拖办理
        if dist["代办"] == "是":
            driver.find_element_by_xpath("/html/body/content/div/form/div/div[2]/div[3]/input[1]").click()
        else:
            driver.find_element_by_xpath("/html/body/content/div/form/div/div[2]/div[3]/input[2]").click()

        # 负责人姓名
        strname = dist["负责人姓名"] + "_" + str(ReadTestFile().readfile())

        driver.find_element_by_id("chargerName").clear()
        driver.find_element_by_id("chargerName").send_keys(strname)

        # 负责人身份证号
        driver.find_element_by_id("chargerIdCard").clear()
        driver.find_element_by_id("chargerIdCard").send_keys(int(dist["负责人身份证号"]))


        driver.find_element_by_id("copy1").click()

        # 选择权利人照片上传
        UploadPic(driver, "/html/body/content/div/form/div/div[6]/div[2]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["身份证电子件"]).upfile(
            "/html/body/content/div/form/div/div[6]/div[2]/div[3]/div[1]/div[1]/div[2]/a")
        # driver.find_element_by_xpath('//*[@id="linkFile1"]').click()
        # try:
        #     os.system(
        #         os.getcwd() + "\\openDiagFrame.exe " + "选择要加载的文件 " + os.getcwd()+"\\inputdata\\TC21.jpg")
        # except:
        #     os.system(
        #         os.getcwd() + "\\openDiagFrame.exe " + "文件上传 " + os.getcwd()+"\\inputdata\\TC21.jpg")
        #
        # driver.find_element_by_xpath('/html/body/content/div/form/div/div[6]/div[2]/input').click()
        # driver.find_element_by_xpath('/html/body/content/div/form/div/div[6]/div[2]/a[2]').click()
        # sleep(1)
        # driver.find_element_by_xpath('/html/body/div[2]/div/div/div[1]/button').click()

        # 联系手机
        driver.find_element_by_id("contactMobile").clear()
        driver.find_element_by_id("contactMobile").send_keys(dist["联系手机"])

        # 联系电话
        driver.find_element_by_id("contactPhone").clear()
        driver.find_element_by_id("contactPhone").send_keys(dist["联系电话"])

        # 电子邮件
        driver.find_element_by_name("obligeeInfo.obligeeEmail").clear()
        driver.find_element_by_name("obligeeInfo.obligeeEmail").send_keys(dist["电子邮件"])

        # 邮编
        driver.find_element_by_id("postCode").clear()
        driver.find_element_by_id("postCode").send_keys(dist["邮编"])

        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[1]/select")).select_by_visible_text(dist["联系地址-省"])
        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[2]/select")).select_by_visible_text(dist["联系地址-市"])
        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[3]/select")).select_by_visible_text(dist["联系地址-区"])

        driver.find_element_by_id("street1").clear()
        driver.find_element_by_id("street1").send_keys(dist["联系地址-街"])
        driver.find_element_by_id("streetNo1").clear()
        driver.find_element_by_id("streetNo1").send_keys(str(dist["联系地址-号"]))

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("contactMobile"))
        sleep(1)

        # 合法有效的证明文件
        driver.find_element_by_id("copy11").click()
        UploadPic(driver, "/html/body/content/div/form/div/div[11]/div[2]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["合法有效存在的证明文件电子件"]).upfile(
            "/html/body/content/div/form/div/div[11]/div[2]/div[3]/div[1]/div[1]/div[2]/a")

        # 委托人相关信息
        if dist["代办"] == "是":

            # 归档
            self.movedownPage(driver, 100)
            sleep(2)

            driver.find_element_by_id("copy12").click()

            driver.find_element_by_id("depositaryName").clear()
            driver.find_element_by_id("depositaryName").send_keys(dist["受托人姓名S"])
            driver.find_element_by_id("depositaryIdCard").clear()
            driver.find_element_by_id("depositaryIdCard").send_keys(int(dist["受托人身份证号S"]))

            # 选择委拖人照片上传
            UploadPic(driver, "/html/body/content/div/form/div/div[12]/div[4]/div[3]/div[1]/div[1]/div[2]/div",
                      dist["身份证电子件S"]).upfile(
                "/html/body/content/div/form/div/div[12]/div[4]/div[3]/div[1]/div[1]/div[2]/a")

            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step1"))

            # 联系手机
            driver.find_element_by_id("contactMobile1").clear()
            driver.find_element_by_id("contactMobile1").send_keys(dist["联系手机S"])

            # 联系电话
            driver.find_element_by_id("contactPhone1").clear()
            driver.find_element_by_id("contactPhone1").send_keys(dist["联系电话S"])

            # 电子邮件
            driver.find_element_by_name("baileeInfo.baileeEmail").clear()
            driver.find_element_by_name("baileeInfo.baileeEmail").send_keys(dist["电子邮件S"])

            # 邮编
            driver.find_element_by_name("baileeInfo.postCode").clear()
            #driver.find_element_by_name("baileeInfo.postCode").send_keys(dist["邮编S"])


            Select(driver.find_element_by_xpath("/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[1]/select")).select_by_visible_text(dist["联系地址-省S"])
            Select(driver.find_element_by_xpath("/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[2]/select")).select_by_visible_text(dist["联系地址-市S"])
            Select(driver.find_element_by_xpath("/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[3]/select")).select_by_visible_text(dist["联系地址-区S"])

            # 地址
            driver.find_element_by_id("baileeStreetName").clear()
            driver.find_element_by_id("baileeStreetName").send_keys(dist["联系地址-街S"])

            driver.find_element_by_id("baileeStreetNo").clear()
            driver.find_element_by_id("baileeStreetNo").send_keys(dist["联系地址-号S"])


        driver.find_element_by_id("textarea1").clear()
        driver.find_element_by_id("textarea1").send_keys(dist["备注"])

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step1"))

        try:
            driver.find_element_by_id("next-step1").click()
        except Exception as e1:
            print(e1)

        # 不动产信息
        self.dbt.changeGotoInfoMessage(driver, dist["不动产类型"], dist["不动产登记类型用例"])

        # 不动产类型信息
        self.dbt.changeGotoWTType(driver, dist["委托类型"], dist["委托类型用例"])

        sleep(1)
        User().Logout(self.driver)
        # driver.find_element_by_xpath("/html/body/header/div[2]/div/ul/li[3]/a").click()
        # 走各种流程
        WorkFlow(self.driver, ReadTestFile().get_danganno()[3:], "其他组织")
        User().Login(self.driver)

    # 封装一下，支持两种传入，一种是位置，另一种是控件显示
    def movedownPage(self, driver, lineNo=None, elementID=None):
        if driver == "":
            driver = self.driver

        if lineNo is not None:
            js = "var q=document.documentElement.scrollTop=" + str(lineNo)
            driver.execute_script(js)

        if lineNo is None and elementID is not None:
            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step1"))

        driver.implicitly_wait(5)

    def tearDown(self):
        pass
        #self.driver.quit()
        #self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
